package com.yuanli.cosmeticmirror.view.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.comenjoysoft.baselibrary.util.AppUtils;
import com.comenjoysoft.baselibrary.util.DateUtil;
import com.comenjoysoft.baselibrary.util.SPUtils;
import com.kongzue.dialog.v3.CustomDialog;
import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseActivity;
import com.yuanli.cosmeticmirror.base.Constant;
import com.yuanli.cosmeticmirror.base.MyApp;
import com.yuanli.cosmeticmirror.bean.MemberBean;
import com.yuanli.cosmeticmirror.bean.MemberInfoBean;
import com.yuanli.cosmeticmirror.model.iview.IMemberView;
import com.yuanli.cosmeticmirror.persenter.MemberPersenter;
import com.yuanli.cosmeticmirror.view.adapter.MemberAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import comenjoy.com.imageloadlibrary.GlideUtil;

/**
 * 会员认证界面
 */
public class MemberActivity extends BaseActivity implements IMemberView {
    @BindView(R.id.member_back)
    ImageView memberBack;
    @BindView(R.id.member_title)
    ConstraintLayout memberTitle;
    @BindView(R.id.member_img)
    ImageView memberImg;
    @BindView(R.id.member_grade)
    TextView memberGrade;
    @BindView(R.id.member_seekbar)
    ProgressBar memberSeekbar;
    @BindView(R.id.member_card_ll)
    ConstraintLayout memberCardLl;
    @BindView(R.id.user_photo)
    LinearLayout userPhoto;
    @BindView(R.id.member_ll)
    LinearLayout memberLl;
    @BindView(R.id.member_tv)
    TextView memberTv;
    @BindView(R.id.member_wechat_cb)
    AppCompatCheckBox memberWechatCb;
    @BindView(R.id.member_rv)
    RecyclerView member_rv;
    @BindView(R.id.member_alipay_cb)
    AppCompatCheckBox memberAlipayCb;
    @BindView(R.id.member_alipay)
    LinearLayout memberAlipay;
    @BindView(R.id.member_wechat)
    LinearLayout memberWechat;
    @BindView(R.id.member_pay)
    TextView memberPay;
    private MemberPersenter persenter;
    private MemberAdapter adapter;
    private static boolean isSelected = false;
    private List<MemberBean> beans;
    private int i;
    private long mLastClickTime = 0;
    public final long TIME_INTERVAL = 1000L;
    private boolean isFirst = false;

    @Override
    protected int getContentView() {
        return R.layout.activity_member;
    }

    @Override
    protected void init(Bundle bundle) {
        persenter = new MemberPersenter(this, this);
    }

    @Override
    protected void initView() {
        String photo = SPUtils.getInstance().getString(Constant.SP_USER_PHOTO_NET);
        if (TextUtils.isEmpty(photo)) {
            GlideUtil.getInstance().loadCircleImage(MemberActivity.this, R.drawable.default_head_img, memberImg);
        } else {
            GlideUtil.getInstance().loadCircleImage(MemberActivity.this, photo, R.drawable.default_head_img, memberImg);
        }

        adapter = new MemberAdapter();
        member_rv.setAdapter(adapter);

        adapter.setOnItemClickListener((adapter, view, position) -> {
            i = position;
            beans = (List<MemberBean>) adapter.getData();
            for (int i = 0; i < adapter.getData().size(); i++) {
                if (i != position) {
                    beans.get(i).setCheck(false);
                } else {
                    beans.get(i).setCheck(true);
                }
            }
            adapter.notifyItemRangeChanged(0, adapter.getItemCount());
        });
        memberAlipayCb.setOnCheckedChangeListener((buttonView, isChecked) -> {
            memberAlipayCb.setChecked(isChecked);
            memberWechatCb.setChecked(!isChecked);
        });
        memberWechatCb.setOnCheckedChangeListener((buttonView, isChecked) -> {
            memberAlipayCb.setChecked(!isChecked);
            memberWechatCb.setChecked(isChecked);
        });
    }

    @Override
    protected void initData() {
        showLoadDialog();
        persenter.getVipInfo();
    }

    @Override
    protected void destroy() {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @OnClick({R.id.member_back, R.id.member_img, R.id.member_alipay, R.id.member_wechat, R.id.member_pay})
    public void onViewClicked(View view) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - mLastClickTime < TIME_INTERVAL) {
            // do something
            return;
        }
        mLastClickTime = nowTime;
        switch (view.getId()) {
            case R.id.member_back:
                finish();
                break;
            case R.id.member_img:
                break;
            case R.id.member_alipay:
                memberAlipayCb.setChecked(true);
                break;
            case R.id.member_wechat:
                memberWechatCb.setChecked(true);
                break;
            case R.id.member_pay:
                if (beans == null) {
                    showToast("请选择充值金额");
                } else if (i < 0) {
                    showToast("请选择充值金额");
                } else {
                    if (memberWechatCb.isChecked()) {
                        if (!AppUtils.isWeixinAvilible(MemberActivity.this)) {

                            showToast("未安装微信或微信版本过低");
                            return;
                        }
                        getCustomDialog();
                        return;
                    }
                    if (!AppUtils.isAliPayInstalled(MemberActivity.this)) {
                        showToast("未安装支付宝或支付宝版本过低");
                        return;
                    }
                    getCustomDialog();
                }

                break;
            default:
                break;
        }
    }

    @Override
    public void onSucess(List<MemberBean> beans) {
        dialogDismiss();
        adapter.setNewInstance(beans);
    }

    @Override
    public void Error(int code) {
        if (code == MemberPersenter.MEMBER_PRICES) {
            dialogDismiss();
            showToast("加载充值选项错误");
        } else if (code == MemberPersenter.MEMBER_WECHAT) {
            showToast("微信支付失败，请重试");
        } else if (code == MemberPersenter.MEMBER_ALIPAY) {
            showToast("支付宝支付失败，请重试");
        } else if (code == MemberPersenter.MEMBER_VIP) {
            dialogDismiss();
            showToast("获取会员信息失败");
        }

    }

    @Override
    public void paySucess(int memberWechat) {
        showLoadDialog();
        persenter.getVipInfo();
        if (memberWechat == MemberPersenter.MEMBER_WECHAT) {
            showToast("微信支付成功");
        } else if (memberWechat == MemberPersenter.MEMBER_ALIPAY) {
            showToast("支付宝支付成功");
        }

    }

    @Override
    public Context getActivity() {
        return this;
    }

    @Override
    public void onVipSucess(MemberInfoBean object) {
        if (object != null) {
            if (!isFirst) {
                isFirst = true;
                persenter.getPrices();
            } else {
                dialogDismiss();
            }
            if (!TextUtils.isEmpty(object.getCreateTime()) && !TextUtils.isEmpty(object.getEndTime())) {
                String allDay = DateUtil.getTwoDay(object.getEndTime().substring(0, 10), object.getCreateTime().substring(0, 10));
                memberSeekbar.setMax(Integer.parseInt(allDay));
                memberSeekbar.setProgress(Integer.parseInt(allDay)-object.getDay());
            }
            memberGrade.setText(TextUtils.isEmpty(object.getMark()) ? "普通用户" : object.getMark());
        }

        MyApp.IsVIP = object != null && object.getDay() > 0;
    }

    /**
     * 会员支付界面  充值
     *
     * @return
     */
    private CustomDialog getCustomDialog() {
        return CustomDialog.show(MemberActivity.this, R.layout.dialog_pay, (dialog, v) -> {
            TextView btnOk = v.findViewById(R.id.pay_enter);
            TextView btncancel = v.findViewById(R.id.pay_cancel);
            btnOk.setOnClickListener(v1 -> {
                persenter.pay(memberWechatCb.isChecked(), beans.get(i).getPrice());
                dialog.doDismiss();
            });
            btncancel.setOnClickListener(v1 -> dialog.doDismiss());
        }).setAlign(CustomDialog.ALIGN.DEFAULT);
    }
}
